// JavaScript Document
/*
$('.reply-message').focus(function () {
    if ($('#used').text() == 0) {
        alert("权限不足。验证邮箱并完善你的个人信息之后方可开启留言评论功能。");
        $('.reply-message').attr('disabled', '');
        return false;
    }
})
*/
$('.reply-message').keyup(function () {
    if ($(this).val() != '') {
        $(this).parent().find('.button').removeAttr('disabled');
    }
    else {
        $(this).parent().find('.button').attr('disabled', '');
    }
})

$('.load-more-reply-btn').click(function () {
    thread_id = $(this).parent().find('.thread-id').text();
    current_user_id = $(this).parent().find('.current-user-id').text();
    my = $(this).parent().find('.my').text();
    $.ajax({
        type: 'POST',
        url: base_url_user + 'messageboard/load_more_reply',
        data: {
            thread_id: thread_id
        },
        success: function (data) {
            allobj = JSON.parse(data);
            $('#feed-reply-holder-' + thread_id).empty();
            for (i = 1; i < allobj.length; i = i + 1) {
                obj = allobj[i];
                reply = '<div class="row collpase feed-reply"><div><div>' +
                        '<a href="' + base_url_user + 'profile/u/' + obj.sender_id + '"><img class="left" src="' + obj.sender_image + '"></a>' +
                        '<div class="name">' +
                        '<span class="message">' +
                        '<a href="' + base_url_user + 'profile/u/' + obj.sender_id + '"><b>' + obj.sender_name + '</b></a>&nbsp;';
                if (obj.private == 1) {
                    reply = reply + '<img title="悄悄话" src="' + base_url + 'assets/img/private.png">';
                }
                reply = reply + '回复 ' +
                        obj.target_name + '：' + obj.message + '</span>' +
                        '<span>';
                if (current_user_id != 0 && obj.sender_id != current_user_id) {
                    reply = reply + ' <a class="reply-btn">回复</a>';
                }
                if (obj.sender_id == current_user_id || my == 1) {
                    reply = reply + ' <a class="delete-message-btn">删除</a>';
                }
                reply = reply +
                        '<div class="hidden thread-id">' + obj.thread_id + '</div>' +
                        '<div class="hidden sender-name">' + obj.sender_name + '</div>' +
                        '<div class="hidden sender-id">' + obj.sender_id + '</div>' +
                        '<div class="hidden notice-id">' + obj.id + '</div>' +
                        '</span>' +
                        '</div>' +
                        '</div></div></div>';
                $('#feed-reply-holder-' + thread_id).append(reply);
            }
            $('#endjs').empty();
            $('#endjs').append('<script type="text/javascript" src="' + base_url_user + 'assets/js/replydelete.js"></script>');
        },
        dataType: 'text',
        cache: false
    });
})


$('#send-message-btn').click(function () {
    if ($('#message').val() != '') {
        $('#send-message-success').hide();
        $('#send-message-loading').css('display', 'inline-block');
        private = $('#private').is(":checked");
        priv = private ? 1 : 0;
        message = $('#house-name').text() != '' ? '我对你发布的房源 ' + $('#house-name').text() + ' 感兴趣！' + $('#message').val() : '' + $('#message').val();
        $.ajax({
            type: 'POST',
            url: base_url_user + 'interact/message1',
            data: {
                target_user_id: $('#user-id').text(),
                message: message,
                private: priv
            },
            success: function (data) {
                $('#message').val('');
                $('#send-message-btn').attr('disabled', '');
                $('#send-message-loading').hide();
                $('#send-message-success').css('display', 'inline-block');
                $('#send-message-success').fadeOut(3400);
                obj = JSON.parse(data);
                reply = '<div class="start-loc"></div><div class="large-12 columns feed"><hr><div class="anchor" id="thread_' + obj.thread_id + '"></div>' +
                        '<div class="content panel">' +
                        '<div class="img-wrapper">' +
                        '<a href="' + base_url_user + 'profile/u/' + obj.sender_id + '"><img class="radius left" src="' + obj.sender_image + '"></a>' +
                        '</div>' +
                        '<div class="message-form">' +
                        '<div class="name">' +
                        '<span>' +
                        '<a href="' + base_url_user + 'profile/u/' + obj.sender_id + '">' +
                        '<b>' + obj.sender_name + '</b></a> (' + obj.time + ')' +
                        '</span>' +
                        '</div>' +
                        '<div class="message-holder">' +
                        '<span class="message">';
                if (obj.private == 1) {
                    reply = reply + '<img title="悄悄话" src="' + base_url + 'assets/img/private.png">';
                }
                reply = reply + obj.message + ' </span>' +
                        '<span>' +
                        '<a class="delete-message-btn">删除</a>' +
                        '<div class="hidden thread-id">' + obj.thread_id + '</div>' +
                        '<div class="hidden notice-id">' + obj.id + '</div>' +
                        '</span>' +
                        '</div><div class="feed-reply-holder"></div>' +
                        '</div>' +
                        '</div>' +
                        '</div>';
                $('.start-loc').before(reply);
                $('.start-loc').last().remove();
                $('#endjs').empty();
                $('#endjs').append('<script type="text/javascript" src="' + base_url_user + 'assets/js/replydelete.js"></script>');
            },
            dataType: 'text',
            cache: false
        });
    }
})

$('#send-invitation-message-btn').click(function () {
    invite_user_id = $('#user-id').text();
    message = $('#invitation-message').val();
    $('#send-invitation-message-success').hide();
    $('#send-invitation-message-loading').css('display', 'inline-block');
    $('#send-invitation-message-loading').css('visibility', 'visible');
    $.ajax({
        type: 'POST',
        url: base_url_user + 'interact/invite',
        data: {
            invite_user_id: invite_user_id, //被邀请加入的室友id
            message: message
        },
        success: function (data) {
            $('#send-invitation-message-loading').hide();
            $('.invite-user-btn').hide()
            $('.cancel-invite-btn').css('display', 'inline-block');
            $('.block-user-btn').attr('disabled', '');
            $('.cancel-block-btn').click();
            $('a.close-reveal-modal').trigger('click');
            $('#send-invitation-message-success').css('display', 'inline-block');
            $('#send-invitation-message-success').fadeOut(10000);
        },
        dataType: 'text',
        cache: false
    });
});



$('.store-user-btn').click(function () {
    store_user_id = $('#user-id').text();
    $.ajax({
        type: 'POST',
        url: base_url_user + 'favorites/ajax/fav_user/add',
        data: {
            for_id: store_user_id
        },
        datatype:'json',
        success: function (data) {
            if (data.success == true) {
                $('.store-user-btn').hide();
                $('.already-store-user-btn').css('display', 'inline-block');
                $('.cancel-store-user-btn').css('display', 'inline-block');
            } else {
                alert(data.msg);
            }
        },
        cache: false
    });
})

$('.cancel-store-user-btn').click(function () {
    store_user_id = $('#user-id').text();
    $.ajax({
        type: 'POST',
        url: base_url_user + 'favorites/ajax/fav_user/remove',
        data: {
            for_id: store_user_id
        },
        datatype:'json',
        success: function (data) {
            if (data.success == true) {
                $('.store-user-btn').css('display', 'inline-block');
                $('.already-store-user-btn').hide();
                $('.cancel-store-user-btn').hide();
            } else {
                alert(data.msg);
            }
        },
        cache: false
    });
})

$('.cancel-store-user-roomie-btn').click(function () {
    store_user_id = $(this).parent().parent().parent().find('.user_id').html();
    $(this).parents('.house').hide();
    $.ajax({
        type: 'POST',
        url: base_url_user + 'favorites/ajax/fav_user/remove',
        data: {
            for_id: store_user_id
        },
        datatype:'json',
        success: function (data) {
            if (data.success == true) {
                $(this).parent().parent().parent().parent().parent().hide();
            } else {
                alert(data.msg);
            }
        },
        cache: false
    });
})

$('.cancel-invite-btn').click(function () {
    invite_user_id = $('#user-id').text();
    $.ajax({
        type: 'POST',
        url: base_url_user + 'interact/cancel_invite',
        data: {
            invite_user_id: invite_user_id
        },
        datatype:'json',
        success: function (data) {
            $('.invite-user-btn').css('display', 'inline-block');
            $('.cancel-invite-btn').hide();
            $('.block-user-btn').removeAttr('disabled');
        },
        dataType: 'text',
        cache: false
    });
})

$('.remove-roomie-btn').click(function () {
    $('.remove-roomie-btn').hide();
    $.ajax({
        type: 'POST',
        url: base_url_user + 'notice/remove_all_roomie',
        data: {
            confirm_remove: 'of_course'
        },
        success: function (data) {

            $('#roomie-holder').hide();
        },
        dataType: 'text',
        cache: false
    });
})




$('.block-user-btn').click(function () {
    if ($(this).attr('disabled') != null) {
        return false;
    }
    block_user_id = $('#user-id').text();
    $.ajax({
        type: 'POST',
        url: base_url_user + 'favorites/ajax/block_user/add',
        data: {
            for_id: block_user_id
        },
        datatype:'json',
        success: function (data) {
            if (data.success == true) {
                $('.block-user-btn').hide()
                $('.cancel-block-btn').css('display', 'inline-block');
            } else {
                alert(data.msg);
            }
        },
        cache: false
    });
})

$('.cancel-block-btn').click(function () {
    block_user_id = $('#user-id').text();
    $.ajax({
        type: 'POST',
        url: base_url_user + 'favorites/ajax/block_user/remove',
        data: {
            for_id: block_user_id
        },
        datatype:'json',
        success: function (data) {
            if (data.success == true) {
                $('.block-user-btn').css('display', 'inline-block');
                $('.cancel-block-btn').hide();
            } else {
                alert(data.msg);
            }
        },
        cache: false
    });
})

$('.accept-invite-btn').click(function () {
    notice_id = $(this).parent().find('.notice-id').text();
    user_id = $('#user-id').text();
    $('#accept-invite-loading').css('visibility', 'visible');
    $.ajax({
        type: 'POST',
        url: base_url_user + 'notice/accept_invitation',
        data: {
            notice_id: notice_id,
            roomie_id: user_id
        },
        success: function (data) {
            obj = JSON.parse(data);
            $('#accept-invite-loading').hide();
            $('.accept-invite-btn').hide();
            if (obj.accept == 1) {
                $('#invitation-success').text('室友添加成功');
                $('#invitation-success').css('display', 'inline-block');
                roomie_holder = '<div class="title small-3 columns"><span class="label search-result-label">' +
                        $('#user-he').text() + '的室友：' +
                        '</span></div><div class="small-9 left columns">';
                for (var i = 0; i < obj.roomies.length; i = i + 1) {
                    var r = obj.roomies[i];
                    roomie_holder = roomie_holder + '<span class="label search-result-label" title="' + r['name'] + ' ' + r['recent_status'] + '">' +
                            '<a target="_blank" href="' + base_url_user + 'profile/u/' + r['id'] + '">' +
                            '<img class="left" src="' + r['image'] + '">' +
                            '</a>' +
                            '</span>&nbsp;'
                }
                roomie_holder = roomie_holder + '</div>';
                $('#roomie-holder').empty();
                $('#roomie-holder').append(roomie_holder);
                $('#invite-btn-holder').hide();
                $('.int-btn').hide();
            }
            else {
                $('#invitation-success').text('接受成功，正等待你的其他室友接受其邀请');
                $('#invitation-success').css('display', 'inline-block');
                $('.int-btn').hide();
            }
        },
        dataType: 'text',
        cache: false
    });
})
function ricegroup(e) {
    var user_id = e.getAttribute("t");
    getricegroup(user_id);
}
function getricegroup(user_id) {
    $.ajax({
        type: 'POST',
        url: base_url_user + 'dinner/store_dinner',
        data: {
            user_id: user_id
        },
        success: function (data) {
            if (data == -1)
                alert('已超过可约团饭人数');
            else if (data != 0) {
                var strarr = data.split(",");
                var i = 0;
                for (i = 0; i < strarr.length - 1; i++) {
                    $('#rice-group-' + strarr[i]).hide();
                    $('#cancel-rice-group-' + strarr[i]).show();
                    if (i > 1)
                        $('#sending').html('发送完成');
                }
                popupwindows(strarr.length - 1);
            }
        },
        dataType: 'text',
        cache: false
    });
}
function cannelricegroup(e) {
    var user_id = e.getAttribute("t");
    $.ajax({
        type: 'POST',
        url: base_url_user + 'dinner/cancel_store_dinner',
        data: {
            user_id: user_id
        },
        success: function (data) {
            if (data == -1)
                popupwindowneed('没有约此人');
            else if (data == 1) {
                $('#rice-group-' + user_id).show();
                $('#cancel-rice-group-' + user_id).hide();
            }
        },
        dataType: 'text',
        cache: false
    });
}
function popupwindows(num) {
    $('#rp-record').text(parseInt($('#rp-record').text()) - parseInt(num));
    $('#popup').css('width', '210px');
    $('#popup').text("约" + num + "人团饭");
    $('#popup').stop(true, true).fadeOut();
    $('#popup').show();
    $('#popup').fadeOut(4000);
}
function popupwindowneed(mess) {
    $('#popup').css('width', '210px');
    $('#popup').text(mess);
    $('#popup').stop(true, true).fadeOut();
    $('#popup').show();
    $('#popup').fadeOut(4000);
}

$('.accepts-dinner-btn').click(function () {
    var dinnerid = $(this).attr('t');
    $.ajax({
        type: 'POST',
        url: base_url_user + 'dinner/pass_dinner',
        data: {
            dinnerid: dinnerid
        },
        success: function (data) {
            $('.cancel-rice-group').hide();
            $('.accepts-dinner-btn').hide();
            $('.ignore-dinner-btn').hide();
            $('.pass-dinner-btn').show();
        },
        dataType: 'text',
        cache: false
    });
})
$('.ignore-dinner-btn').click(function () {
    var dinnerid = $(this).attr('t');
    $.ajax({
        type: 'POST',
        url: base_url_user + 'dinner/ignore_dinner',
        data: {
            dinnerid: dinnerid
        },
        success: function (data) {
            $('.cancel-rice-group').hide();
            $('.accepts-dinner-btn').hide();
            $('.ignore-dinner-btn').hide();
            $('.no-pass-dinner-btn').show();
        },
        dataType: 'text',
        cache: false
    });
})
$('.get-show-type').click(function () {
    var type = $(this).attr('t');
    $("#" + type).toggle();
    var test = $(this).html();
    if (test == '查看全部')
        $(this).html('收起');
    else
        $(this).html('查看全部');
});

$('#send-application-message-btn').click(function(){
	house_id = $('#house-id').text();
	message = $('#application-message').val();
	$('#send-application-message-success').hide();
	$('#send-application-message-loading').css('display', 'inline-block');
	$('#send-application-message-loading').css('visibility','visible');
	$.ajax({
		type: 'POST',
		url: base_url+'interact/apply', 
		data: {
				house_id: house_id,
				message: message
			},
		success: function(data){
			$('#send-application-message-loading').hide();
			$('#send-application-message-success').css('display', 'inline-block');
		}, 
		dataType: 'text',  
		cache: false
		});
});

$('#send-report-message-btn').click(function(){
	house_id = $('#house-id').text();
	message = $('#report-message').val();
	$('#send-report-message-success').hide();
	$('#send-report-message-loading').css('display', 'inline-block');
	$('#send-report-message-loading').css('visibility','visible');
	$.ajax({
		type: 'POST',
		url: base_url+'interact/report', 
		data: {
				house_id: house_id,
				message: message
			},
		success: function(data){
			$('#send-report-message-loading').hide();
			$('#send-report-message-success').css('display', 'inline-block');
			$('#send-report-message-success').fadeOut(3400);
		}, 
		dataType: 'text',  
		cache: false
		});
});


$('.store-house-btn').click(function(){
    user_id = document.getElementById('user_session').value;
    if(user_id == ""){
        //alert(111);return false;
        window.location.href=base_url_www + 'user/login';
        return false;
    }
	house_id = $('#house-id').text();
	$.ajax({
		type: 'POST',
		url:base_url_www+"interact/store_house", 
		data: {
			house_id: house_id
		},
		success: function(data){
            //console.log(111);
			$('.store-house-btn').hide();
			$('.cancel-store-house-btn').css('display','inline-block');
		}, 
		dataType: 'text',  
		cache: false
		});
})

$('.cancel-store-house-btn').click(function(){
	house_id = $('#house-id').text();	
	$.ajax({
		type: 'POST',
		url: base_url_www+'interact/cancel_store_house', 
		data: {
			house_id: house_id
		},
		success: function(data){
			$('.store-house-btn').css('display','inline-block');
			$('.cancel-store-house-btn').hide();
		}, 
		dataType: 'text',  
		cache: false
		});
})

